package servlet;

import model.Goods;
import model.Share;
import model.User;
import service.GoodsService;
import service.ShareService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 分享统计Servlet
 */
@WebServlet(name = "share_stat", urlPatterns = "/share_stat")
public class ShareStatServlet extends HttpServlet {

    private ShareService shareService = new ShareService();
    private GoodsService goodsService = new GoodsService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取当前登录用户
        User user = (User) request.getSession().getAttribute("user");
        
        // 获取热门分享商品
        List<Integer> hotGoodsIds = shareService.getHotShareGoodsIds(10);
        List<Goods> hotShareGoodsList = new ArrayList<>();
        
        // 获取总分享统计
        int totalShareCount = 0;
        int wechatShareCount = 0;
        int weiboShareCount = 0;
        int qqShareCount = 0;
        
        // 填充热门商品分享数据
        if (hotGoodsIds != null && !hotGoodsIds.isEmpty()) {
            for (Integer goodsId : hotGoodsIds) {
                Goods goods = goodsService.getGoodsById(goodsId);
                if (goods != null) {
                    // 获取该商品的分享统计
                    int shareCount = shareService.getShareCount(goodsId);
                    int goodsWechatShareCount = shareService.getShareCountByType(goodsId, "wechat");
                    int goodsWeiboShareCount = shareService.getShareCountByType(goodsId, "weibo");
                    int goodsQQShareCount = shareService.getShareCountByType(goodsId, "qq");
                    int goodsLinkShareCount = shareService.getShareCountByType(goodsId, "link");
                    
                    // 设置分享数据
                    goods.setShareCount(shareCount);
                    goods.setWechatShareCount(goodsWechatShareCount);
                    goods.setWeiboShareCount(goodsWeiboShareCount);
                    goods.setQqShareCount(goodsQQShareCount);
                    goods.setLinkShareCount(goodsLinkShareCount);
                    
                    // 汇总总分享数据
                    totalShareCount += shareCount;
                    wechatShareCount += goodsWechatShareCount;
                    weiboShareCount += goodsWeiboShareCount;
                    qqShareCount += goodsQQShareCount;
                    
                    hotShareGoodsList.add(goods);
                }
            }
        }
        
        // 获取用户分享记录
        List<Share> userShareList = null;
        if (user != null) {
            userShareList = shareService.getUserShares(user.getId());
            // 填充分享关联的商品信息
            if (userShareList != null) {
                for (Share share : userShareList) {
                    share.setGoods(goodsService.getGoodsById(share.getGoods_id()));
                }
            }
        }
        
        // 设置请求属性
        request.setAttribute("hotShareGoodsList", hotShareGoodsList);
        request.setAttribute("totalShareCount", totalShareCount);
        request.setAttribute("wechatShareCount", wechatShareCount);
        request.setAttribute("weiboShareCount", weiboShareCount);
        request.setAttribute("qqShareCount", qqShareCount);
        request.setAttribute("userShareList", userShareList);
        
        // 转发到分享统计页面
        request.getRequestDispatcher("/share_stat.jsp").forward(request, response);
    }
} 